package guda.house.web.action.bill;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BillElecBiz;
import guda.house.biz.OwnerBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.bill.BillElecEditForm;
import guda.house.biz.form.bill.BillElecForm;
import guda.house.biz.query.BillElecQuery;
import guda.house.biz.query.RoleQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.BillElecDAO;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 15/11/15.
 */
@Controller
@RequestMapping(value = "bill/elec")
public class BillElecAction {

    @Autowired
    private BillElecBiz billElecBiz;
    @Autowired
    private OwnerBiz ownerBiz;
    @Autowired
    private BillElecDAO billElecDAO;

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        BillElecQuery billElecQuery = new BillElecQuery();
        billElecQuery.setPageNo(pageId);
        billElecQuery.setPageSize(pageSize);
        modelMap.put("query", billElecQuery);
        BizResult bizResult = billElecBiz.list(billElecQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/elec/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, BillElecEditForm billElecEditForm,
                       BindingResult result, Map<String, Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = billElecBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            BillElecDO billElecDO = (BillElecDO) bizResult.data.get("billElecDO");
            billElecEditForm.initForm(billElecDO);

            BizResult bizResult1 = ownerBiz.queryForEdit(billElecDO.getSuiteId());
            modelMap.putAll(bizResult1.data);

            SuiteDO suiteDO =  (SuiteDO)bizResult1.data.get("suiteDO");
            UnitDO unitDO = (UnitDO)bizResult1.data.get("unitDO");
            BuildingDO buildingDO = (BuildingDO)bizResult1.data.get("buildingDO");
            billElecEditForm.setVillageId(buildingDO.getVillageId());
            billElecEditForm.setBuildingId(buildingDO.getBuildingId());
            billElecEditForm.setUnitId(unitDO.getUnitId());
            billElecEditForm.setSuiteId(suiteDO.getSuiteId());

            return "bill/elec/edit.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }


    @RequestMapping(value = "/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, BillElecForm billElecForm,
                         BindingResult result) {
        return "bill/elec/create.vm";
    }

    @RequestMapping(value = "/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap, @Valid BillElecForm billElecForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = ownerBiz.queryForCreateReset(billElecForm.getVillageId(), billElecForm.getBuildingId(), billElecForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "bill/elec/create.vm";
        }
        if(billElecForm.getRecordCurrent() - billElecForm.getRecordLast() < 0){
            result.rejectValue("recordCurrent","recordCurrentError","本月读数应该大于上月读数");
            BizResult bizResult = ownerBiz.queryForCreateReset(billElecForm.getVillageId(), billElecForm.getBuildingId(), billElecForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "bill/elec/create.vm";
        }

        BizResult bizResult = billElecBiz.create(billElecForm);
        if (bizResult.success) {
            return "redirect:/bill/elec/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, @Valid BillElecEditForm billElecEditForm,
                           BindingResult result, Map<String, Object> model) {
        if (result.hasErrors()) {
            return "bill/elec/edit.vm";
        }
        BillElecDO billElecDO = billElecEditForm.toDO();
        BizResult bizResult = billElecBiz.update(billElecEditForm);
        if (bizResult.success) {
            return "redirect:/bill/elec/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = billElecBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }

    @RequestMapping(value = "/getLatest.htm")
    public void getLatest(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long suiteId = RequestUtil.getLong(request, "suiteId");
        BillElecDO billElecDO = billElecDAO.queryLatest(suiteId);

        ResponseHelper.ajaxReturn(billElecDO, response);
        return;


    }
}
